﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using NDFace.BLL; // 引入自定义的命名空间  

namespace NDFace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                // 创建FaceRecognition对象
                var faceRecognition = FaceRecognition.Instance;

                //// 准备训练数据
                //List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
                //List<int> labels = new List<int>();

                //// 假设我们有一些训练图像和对应的标签
                //// 这里需要替换为实际的图像路径和标签
                //trainingImages.Add(new Image<Gray, byte>("path_to_image1.jpg"));
                //labels.Add(1); // 标签1

                //trainingImages.Add(new Image<Gray, byte>("path_to_image2.jpg"));
                //labels.Add(2); // 标签2

                //// 训练识别器
                //faceRecognition.TrainRecognizer(trainingImages, labels);

                // 加载要检测和识别的图像
                var testImage = new Image<Bgr, byte>(/*"path_to_test_image.jpg"*//*@"D:\Test00\女2.JPG"*/"mayun.jpg");

                // 检测人脸
                List<Rectangle> faces = faceRecognition.DetectFaces(testImage);

                foreach (var face in faces)
                {
                    // 从检测到的人脸区域裁剪出人脸图像
                    var faceImage = testImage.Copy(face).Convert<Gray, byte>();

                    // 识别人脸
                    int label = faceRecognition.RecognizeFace(faceImage);

                    // 在图像上绘制矩形和标签
                    testImage.Draw(face, new Bgr(Color.Red), 2);
                    CvInvoke.PutText(testImage, $"Label: {label}", new Point(face.X, face.Y - 10), Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.5, new Bgr(Color.Green).MCvScalar);
                }

                // 显示结果
                CvInvoke.Imshow("Face Recognition", testImage);
                CvInvoke.WaitKey(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Err:{ex.Message}");
            }
        }
    }
}
